﻿using DataHelper;
using SelfServiceWriteSystem.Mode;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace HandyControlProject5
{
    /// <summary>
    /// EventList.xaml 的交互逻辑
    /// </summary>
    public partial class EventList : UserControl, INotifyPropertyChanged
    {

        MainWindow mainWindow;


        List<Project> Projects = new List<Project>();
        List<DepartmentProject> DepartmentProjects = new List<DepartmentProject>();
        List<SubjectProject> SubjectProjects = new List<SubjectProject>();

        List<ShowItem> ProjectsShowItems = new List<ShowItem>();
        List<ShowItem> DepartmentsShowItems = new List<ShowItem>();
        List<ShowItem> SubjectsShowItems = new List<ShowItem>();

        int ProjectPage = 1;
        int DepartmentPage = 1;
        int SubjectPage = 1;

        int ProjectPageTotal = 1;
        int DepartmentPageTotal = 1;
        int SubjectPageTotal = 1;

        int ProjectOnePageNumber = 10;
        int DepartmentOnePageNumber = 12;
        int SubjectOnePageNumber = 44;

        public void ChangePage0(ListBox listBox, TextBlock textBlock, ref List<Project> projects, ref List<ShowItem> showItems, int page, int OnePageNumber)
        {
            int total = (int)Math.Ceiling(projects.Count / (double)OnePageNumber);
            if (page >= total)
                page = total;
            textBlock.Text = "第  " + page.ToString() + " / " + total.ToString() + "  页";
            ProjectPageTotal = total;
            showItems.Clear();
            for (int i = (page - 1) * OnePageNumber; i < (page) * OnePageNumber; i++)
            {
                if (i < projects.Count)
                {
                    showItems.Add(new ShowItem
                    {
                        Index = i + 1,
                        Name = projects[i].Name,
                        tag = projects[i]
                    });
                }
            }
        }
        public void ChangePage1(ListBox listBox, TextBlock textBlock, ref List<Department> departments, ref List<ShowItem> showItems, int page, int OnePageNumber)
        {
            int total = (int)Math.Ceiling(departments.Count / (double)OnePageNumber);
            if (page >= total)
                page = total;
            textBlock.Text = "第  " + page.ToString() + " / " + total.ToString() + "  页";
            DepartmentPageTotal = total;
            showItems.Clear();
            for (int i = (page - 1) * OnePageNumber; i < (page) * OnePageNumber; i++)
            {
                if (i < departments.Count)
                {
                    showItems.Add(new ShowItem
                    {
                        Index = i + 1,
                        Name = departments[i].Name,
                        tag = departments[i]
                    });
                }
            }
        }
        public void ChangePage2(ListBox listBox, TextBlock textBlock, ref List<Subject> subjects, ref List<ShowItem> showItems, int page, int OnePageNumber)
        {
            int total = (int)Math.Ceiling(subjects.Count / (double)OnePageNumber);
            if (page >= total)
                page = total;
            textBlock.Text = "第  " + page.ToString() + " / " + total.ToString() + "  页";
            SubjectPageTotal = total;
            showItems.Clear();
            for (int i = (page - 1) * OnePageNumber; i < page * OnePageNumber; i++)
            {
                if (i < subjects.Count)
                {
                    showItems.Add(new ShowItem
                    {
                        Index = i + 1,
                        Name = subjects[i].Name,
                        Pic = AppDomain.CurrentDomain.BaseDirectory + subjects[i].Image,
                        tag = subjects[i]
                    });
                }
            }
        }



        public EventList(MainWindow _mainWindow)
        {
            InitializeComponent();

            DataContext = this;
            mainWindow = _mainWindow; 
 

        }

        bool isFromSearch = false;

        private int _count1;
        public int Count1
        {
            get { return _count1; }
            set
            {
                _count1 = value;
                OnPropertyChanged("Count1");
            }
        }

        private String _LabelName;
        public String LabelName
        {
            get { return _LabelName; }
            set
            {
                _LabelName = value;
                OnPropertyChanged("LabelName");
            }
        }

        private String _LabelText;
        public String LabelText
        {
            get { return _LabelText; }
            set
            {
                _LabelText = value;
                OnPropertyChanged("LabelText");
            }
        }

        public void GetEventByDepartment(long id,string name)
        {
            LabelName = "当前部门：";
            LabelText = name;
            isFromSearch = false;
            Projects.Clear();
            DepartmentProjects = DbHelper.GetDepartmentProjects(id);

            foreach (DepartmentProject departmentProject in DepartmentProjects)
            {
                Projects.Add(new Project()
                {
                    Id = departmentProject.ProjectId,
                    Name = departmentProject.ProjectName,
                });
            }

            if(DepartmentProjects.Count <= 0)
                MessageBox.Show("对不起，当前部门下暂无事项数据！请等待更新。。。\n", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Information);

            ChangePage0(ProjectsList, pageTextBlock, ref Projects, ref ProjectsShowItems, ProjectPage, ProjectOnePageNumber);
            ProjectsList.ItemsSource = ProjectsShowItems;
            ProjectsList.Items.Refresh();
            Count1 = Projects.Count;

        }
        public void GetEventBySubject(long id, string name)
        {
            LabelName = "当前分类：";
            LabelText = name;
            isFromSearch = false;

            Projects.Clear();
            SubjectProjects = DbHelper.GetSubjectProjects(id);

            foreach (SubjectProject  subjectProject in SubjectProjects)
            {
                Projects.Add(new Project()
                {
                    Id = subjectProject.ProjectId,
                    Name = subjectProject.ProjectName,
                });
            }

            if (SubjectProjects.Count <= 0)
                MessageBox.Show("对不起，当前分类下暂无事项数据！请等待更新。。。\n", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Information);

            ChangePage0(ProjectsList, pageTextBlock, ref Projects, ref ProjectsShowItems, ProjectPage, ProjectOnePageNumber);
            ProjectsList.ItemsSource = ProjectsShowItems;
            ProjectsList.Items.Refresh();
            Count1 = Projects.Count;
        }

        public void GetEventByDepartmentFormSearch(long id, string name)
        {
            LabelName = "当前部门：";
            LabelText = name;
            isFromSearch = true;

            Projects.Clear();
            DepartmentProjects = DbHelper.GetDepartmentProjects(id);

            foreach (DepartmentProject departmentProject in DepartmentProjects)
            {
                Projects.Add(new Project()
                {
                    Id = departmentProject.ProjectId,
                    Name = departmentProject.ProjectName,
                });
            }

            if (DepartmentProjects.Count <= 0)
                MessageBox.Show("对不起，当前部门下暂无事项数据！请等待更新。。。\n", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Information);

            ChangePage0(ProjectsList, pageTextBlock, ref Projects, ref ProjectsShowItems, ProjectPage, ProjectOnePageNumber);
            ProjectsList.ItemsSource = ProjectsShowItems;
            ProjectsList.Items.Refresh();
            Count1 = Projects.Count;
        }
        public void GetEventBySubjectFormSearch(long id, string name)
        {
            LabelName = "当前分类：";
            LabelText = name;
            isFromSearch = true;

            Projects.Clear();
            SubjectProjects = DbHelper.GetSubjectProjects(id);

            foreach (SubjectProject subjectProject in SubjectProjects)
            {
                Projects.Add(new Project()
                {
                    Id = subjectProject.ProjectId,
                    Name = subjectProject.ProjectName,
                });
            }

            if (SubjectProjects.Count <= 0)
                MessageBox.Show("对不起，当前分类下暂无事项数据！请等待更新。。。\n", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Information);

            ChangePage0(ProjectsList, pageTextBlock, ref Projects, ref ProjectsShowItems, ProjectPage, ProjectOnePageNumber);
            ProjectsList.ItemsSource = ProjectsShowItems;
            ProjectsList.Items.Refresh();
            Count1 = Projects.Count;
        }


        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if(isFromSearch)
                mainWindow.UserContent = mainWindow.searchList;
            else
                mainWindow.UserContent = mainWindow.mainList;
        }

        private void HotDocsList_MouseUp(object sender, MouseButtonEventArgs e)
        {
           
        }


        #region 分页切换
        /// <summary>
        /// 到首页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click1(object sender, RoutedEventArgs e)
        {
            ProjectPage = 1;
            ChangePage0(ProjectsList, pageTextBlock, ref Projects, ref ProjectsShowItems, ProjectPage, ProjectOnePageNumber);
            ProjectsList.Items.Refresh();
        }
        /// <summary>
        /// 前一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (ProjectPage > 1) ProjectPage -= 1;
            ChangePage0(ProjectsList, pageTextBlock, ref Projects, ref ProjectsShowItems, ProjectPage, ProjectOnePageNumber);
            ProjectsList.Items.Refresh();
        }
        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            if (ProjectPage < ProjectPageTotal) ProjectPage += 1;
            ChangePage0(ProjectsList, pageTextBlock, ref Projects, ref ProjectsShowItems, ProjectPage, ProjectOnePageNumber);
            ProjectsList.Items.Refresh();
        }
        /// <summary>
        /// 到尾页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            ProjectPage = ProjectPageTotal;
            ChangePage0(ProjectsList, pageTextBlock, ref Projects, ref ProjectsShowItems, ProjectPage, ProjectOnePageNumber);
            ProjectsList.Items.Refresh();
        }
        #endregion

        private void ProjectsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                var project = ProjectsList.SelectedItem as ShowItem;
                mainWindow.docView.GetDoc((project.tag as Project).Id, project.Name, isFromSearch);
                mainWindow.UserContent = mainWindow.docView;
            }
            catch (Exception ex)
            {
                //MessageBox.Show("对不起，服务异常！\n" + ex.Message, "出错了", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            ProjectsList.SelectedIndex = -1;
        }
    }
}
